package com.coderxq.blog.utils.jwt;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.coderxq.blog.aspect.LogAspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lijingming
 * @Date: 2025/2/10
 * @Description:
 **/
public class JwtUtil {
    private static   final  byte[] key = "hello word".getBytes();
    private static final Logger logger = LoggerFactory.getLogger(JwtUtil.class);

    public static  String createToken(String account,String password){
        Map<String, Object> map = new HashMap<String, Object>() {
            private static final long serialVersionUID = 1L;
            {
                put("account", account);
                put("password", password);
                put("expire_time", String.valueOf(System.currentTimeMillis() + 1000 * 60 * 60) );// 过期时间
            }
        };

        String token =  JWTUtil.createToken(map, key);

        return token;
    }
    public static Map<String,Object> parseTokenInfo(String token){
        Map<String, Object> payloads = JWTUtil.parseToken(token).getPayloads();
            return payloads;
    }

    public static  boolean verifyToken(String token){
       boolean result = true;
       try {
           JWTUtil.verify(token, key);

           Map<String, Object> payloads = JwtUtil.parseTokenInfo(token);
           String account = (String) payloads.get("account");
           String expire_time =(String) payloads.get("expire_time");
           // 将 Long 类型转换为 long 类型
           logger.info("expire_time"+expire_time);
           if (System.currentTimeMillis() > Long.parseLong(expire_time)) {
               logger.info("token已过期");
               result = false;
           }
       }catch (Exception e){
           logger.info("token无效");
           result = false;
       }

        return result;
    }
}
